package com.test.lyt.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyTestController {
    @Autowired
    private SqlSession sqlSession;


    @Test
    public void test() {
        String a = "{\n" +
                "    \"Version\": \"2012-10-17\",\n" +
                "    \"Statement\": [\n" +
                "        {\n" +
                "            \"Effect\": \"Allow\",\n" +
                "            \"Principal\": {\n" +
                "                \"AWS\": [\n" +
                "                    \"*\"\n" +
                "                ]\n" +
                "            },\n" +
                "            \"Action\": [\n" +
                "                \"s3:GetBucketLocation\",\n" +
                "                \"s3:ListBucket\",\n" +
                "                \"s3:ListBucketMultipartUploads\"\n" +
                "            ],\n" +
                "            \"Resource\": [\n" +
                "                \"arn:aws:s3:::dev-public\"\n" +
                "            ]\n" +
                "        },\n" +
                "        {\n" +
                "            \"Effect\": \"Allow\",\n" +
                "            \"Principal\": {\n" +
                "                \"AWS\": [\n" +
                "                    \"*\"\n" +
                "                ]\n" +
                "            },\n" +
                "            \"Action\": [\n" +
                "                \"s3:DeleteObject\",\n" +
                "                \"s3:GetObject\",\n" +
                "                \"s3:ListMultipartUploadParts\",\n" +
                "                \"s3:PutObject\",\n" +
                "                \"s3:AbortMultipartUpload\"\n" +
                "            ],\n" +
                "            \"Resource\": [\n" +
                "                \"arn:aws:s3:::dev-public/*\"\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        JSONObject jsonObject = JSONObject.parseObject(a);
        JSONArray jsonArray = jsonObject.getJSONArray("Statement").getJSONObject(0).getJSONObject("Principal").getJSONArray("AWS");
        Object b = jsonArray.get(0);
        if (b.equals("*")) {
            System.out.println(b);

        } else {
            System.out.println("fffff");
        }

    }

    @Test
    public void test1() {
        boolean rule = fuzzySearch("rule", "rule/aa");
        System.out.println(rule);
    }

    /**
     * 模糊查询
     */
    public boolean fuzzySearch(String input, String keyword) {
        Pattern pattern = Pattern.compile(".*" + input + ".*"); // 构建正则表达式，.* 表示任意数量的任意字符
        Matcher matcher = pattern.matcher(keyword);
        if (matcher.matches()) {
            return true;
        }
        return false;
    }

    @Test
    public void outPathName() {
        String c = "abd/dwug/dfgwi/";
        String b = "/mall/jj.txt";

        String[] parts = c.split("/");
        if (parts.length > 0 && !parts[parts.length - 1].isEmpty()) {
            System.out.println(parts[parts.length - 1]);
        }

    }

    @Test
    public void test2() {
        String instanceList = "[{\"endpointType\":1,\"instanceId\":345,\"instanceName\":\"1\"},{\"endpointType\":2,\"instanceId\":123,\"instanceName\":\"2\"}]";
        JSONArray jsonArray = JSONArray.parseArray(instanceList);


    }

    @Test
    public void test3() {
        // 字符串形式的路径
        String pathStr = "33/cc/17b71000000000003380491";

        // 将字符串转换为Path对象
        Path path = Paths.get(pathStr);
        System.out.println("Path: " + path);

        try {
            // 创建目录
            Path directories = Files.createDirectories(path);
            Path root = directories.getRoot();
            System.out.println("root:" + root);
            Path path1 = directories.toRealPath();
            System.out.println("path1:" + path1);
            Path parent = directories.getParent();
            System.out.println("parent:" + parent);
            FileSystem fileSystem = directories.getFileSystem();
            System.out.println("fileSystem:" + fileSystem);
            Path path2 = directories.toAbsolutePath();
            System.out.println("path2:" + path2);
        } catch (IOException e) {
            System.err.println("Failed to create directory: " + e.getMessage());
        }
    }


    public static void main(String[] args) {
        String path = "33/cc/17b71000000000003380491/ffggh/uu/ewihj/jdjk";
        List<String> list = generatorDir(path);
        System.out.println(list.toArray());
    }

    public static List<String> generatorDir(String path) {
        String[] dirs = path.split("/");
        List<String> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < dirs.length - 1; i++) {
            sb.append(dirs[i]).append("/");
            String dir = sb.toString();
            list.add(dir);
        }
        return list;
    }

    @Test
    public void test4() {
        String userHome = System.getProperty("user.home");
        System.out.println(userHome);
    }

    @Test
    public void test5() throws SQLException {
        Connection connection = sqlSession.getConnection();
        DatabaseMetaData metaData = connection.getMetaData();
        ResultSet columns = metaData.getColumns(null, null, "test", null);
    }

    @Test
    public void test6() {
        String a = "/";
        String b = "/adsfesf";
        if (!b.endsWith("/") || b == "/") {
            System.out.println("yes");
        }
    }

    @Test
    public void test7() {

        LocalDateTime now = LocalDateTime.now();
        System.out.println("now: " + now);

        // 获取前一天的时间
        LocalDateTime previousDay = now.minusDays(1);

        // 定义前一天的开始时间和结束时间
        LocalDateTime startTime = previousDay.truncatedTo(ChronoUnit.DAYS); // 00:00:00 of the previous day
        LocalDateTime endTime = startTime.plusDays(1); // 00:00:00 of the current day (exclusive)

        // 打印结果
        System.out.println("Start Time: " + startTime);
        System.out.println("End Time: " + endTime);

    }

    @Test
    public void test8() {
        try {
            // 创建Calendar实例
            Calendar calendar = Calendar.getInstance();

//            // 设置时间为当前时间
//            Date currentDate = calendar.getTime();
//
//            // 获取当前日期的开始时间
//            calendar.set(Calendar.HOUR_OF_DAY, 0);
//            calendar.set(Calendar.MINUTE, 0);
//            calendar.set(Calendar.SECOND, 0);
//            calendar.set(Calendar.MILLISECOND, 0);
//            Date currentDayStartTime = calendar.getTime();
//
//            // 获取当前日期的结束时间
//            calendar.set(Calendar.HOUR_OF_DAY, 23);
//            calendar.set(Calendar.MINUTE, 59);
//            calendar.set(Calendar.SECOND, 59);
//            calendar.set(Calendar.MILLISECOND, 999);
//            Date currentDayEndTime = calendar.getTime();
//
//            // 获取前一天的日期
            calendar.add(Calendar.DAY_OF_MONTH, -1);
//            Date previousDate = calendar.getTime();

            // 获取前一天的开始时间
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            Date previousDayStartTime = calendar.getTime();

            // 获取前一天的结束时间
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 999);
            Date previousDayEndTime = calendar.getTime();

            // 使用SimpleDateFormat格式化日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//            System.out.println("当前日期的开始时间: " + sdf.format(currentDayStartTime));
//            System.out.println("当前日期的结束时间: " + sdf.format(currentDayEndTime));
            System.out.println("前一天的开始时间: " + sdf.format(previousDayStartTime));
            System.out.println("前一天的结束时间: " + sdf.format(previousDayEndTime));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test9() {
        CompletableFuture.supplyAsync(() -> {
            // 模拟耗时操作
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hello, CompletableFuture!";
        }).thenAccept(result -> {
            System.out.println("Result: " + result);
        }).exceptionally(ex -> {
            ex.printStackTrace();
            return null;
        });

        // 保持主线程运行，以便异步任务可以完成
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test10() {
        ExecutorService executorService = Executors.newFixedThreadPool(2);

        Future<String> future = executorService.submit(() -> {
            // 模拟耗时操作
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hello, ExecutorService!";
        });

        try {
            String result = future.get(); // 阻塞等待结果
            System.out.println("Result: " + result);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

        executorService.shutdown();
    }
    @Test
    public void test11() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5,
                10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
        threadPoolExecutor.execute(() -> {
            System.out.println("Hello, ThreadPoolExecutor!");
        });
        Future<?> submit = threadPoolExecutor.submit(() -> {
            System.out.println("Hello, ThreadPoolExecutor!");
        });
    }

}